package sort

import (
	"fmt"
)

func SelectSort(arr []int){
	if arr == nil || len(arr) < 2 {return}
	var minIndex int
	for i:=0; i<len(arr); i++ {
		minIndex = i
		for j:= i+1; j<len(arr); j++ {
			if arr[j] < arr[minIndex] {
				minIndex = j
			}
		}
		swap(arr, i, minIndex)
	}
}

func BubbleSort(arr []int){
	if arr == nil || len(arr) < 2 {return}
	end := len(arr)-1
	for i:=end; i>=0; i--{
		for second:=1; second<=end; second++ {
			if arr[second] < arr[second-1] {
				swap(arr, second, second-1)
			}
		}
	}
}

func InsertSort(arr []int) {
	if arr == nil || len(arr) < 2 {return}
	for i:=0; i<len(arr); i++ {
		for j, pre := i, i-1; pre>=0 && arr[pre] > arr[j] ; j, pre = j-1, pre-1{
			swap(arr, j, pre)
		}
	}
}

func swap(arr []int, i int, j int) {
	arr[i], arr[j] = arr[j], arr[i]
}


func main(){
	arr := []int{3,1,5,21,45,1,4,7,89}
	fmt.Println(arr)
	InsertSort(arr)
	fmt.Println(arr)
}
